home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH6 / SRC / BDITCH2.FRM < prev    next >
Text File  |  1996-04-24  |  8KB  |  281 lines

  1. VERSION 4.00
  2. Begin VB.Form BDitch2Form 
  3.    Caption         =   "Bowditch 2"
  4.    ClientHeight    =   5670
  5.    ClientLeft      =   2070
  6.    ClientTop       =   930
  7.    ClientWidth     =   4830
  8.    Height          =   6360
  9.    Left            =   2010
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   378
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   322
  14.    Top             =   300
  15.    Width           =   4950
  16.    Begin VB.PictureBox Canvas 
  17.       AutoRedraw      =   -1  'True
  18.       Height          =   4815
  19.       Left            =   0
  20.       ScaleHeight     =   -2.2
  21.       ScaleLeft       =   -1.1
  22.       ScaleMode       =   0  'User
  23.       ScaleTop        =   1.1
  24.       ScaleWidth      =   2.2
  25.       TabIndex        =   13
  26.       Top             =   840
  27.       Width           =   4815
  28.    End
  29.    Begin VB.TextBox QText 
  30.       Height          =   285
  31.       Left            =   3120
  32.       TabIndex        =   10
  33.       Text            =   "5"
  34.       Top             =   45
  35.       Width           =   615
  36.    End
  37.    Begin VB.TextBox PText 
  38.       Height          =   285
  39.       Left            =   2040
  40.       TabIndex        =   9
  41.       Text            =   "4"
  42.       Top             =   45
  43.       Width           =   615
  44.    End
  45.    Begin VB.TextBox ThetaText 
  46.       Height          =   285
  47.       Left            =   4200
  48.       TabIndex        =   7
  49.       Text            =   "30"
  50.       Top             =   480
  51.       Width           =   615
  52.    End
  53.    Begin VB.TextBox YscaleText 
  54.       Height          =   285
  55.       Left            =   2040
  56.       TabIndex        =   5
  57.       Text            =   "0.6"
  58.       Top             =   480
  59.       Width           =   615
  60.    End
  61.    Begin VB.TextBox XscaleText 
  62.       Height          =   285
  63.       Left            =   600
  64.       TabIndex        =   3
  65.       Text            =   "0.9"
  66.       Top             =   480
  67.       Width           =   615
  68.    End
  69.    Begin VB.TextBox DtText 
  70.       Height          =   285
  71.       Left            =   240
  72.       TabIndex        =   2
  73.       Text            =   "0.01"
  74.       Top             =   45
  75.       Width           =   615
  76.    End
  77.    Begin VB.CommandButton CmdGo 
  78.       Caption         =   "Go"
  79.       Default         =   -1  'True
  80.       Height          =   375
  81.       Left            =   4200
  82.       TabIndex        =   0
  83.       Top             =   0
  84.       Width           =   615
  85.    End
  86.    Begin VB.Label Label1 
  87.       Caption         =   "Q"
  88.       Height          =   255
  89.       Index           =   6
  90.       Left            =   2955
  91.       TabIndex        =   12
  92.       Top             =   60
  93.       Width           =   255
  94.    End
  95.    Begin VB.Label Label1 
  96.       Caption         =   "P"
  97.       Height          =   255
  98.       Index           =   4
  99.       Left            =   1920
  100.       TabIndex        =   11
  101.       Top             =   60
  102.       Width           =   255
  103.    End
  104.    Begin VB.Label Label1 
  105.       Caption         =   "Angle (degrees)"
  106.       Height          =   255
  107.       Index           =   5
  108.       Left            =   3000
  109.       TabIndex        =   8
  110.       Top             =   525
  111.       Width           =   1215
  112.    End
  113.    Begin VB.Label Label1 
  114.       Caption         =   "Y scale"
  115.       Height          =   255
  116.       Index           =   3
  117.       Left            =   1440
  118.       TabIndex        =   6
  119.       Top             =   525
  120.       Width           =   615
  121.    End
  122.    Begin VB.Label Label1 
  123.       Caption         =   "X scale"
  124.       Height          =   255
  125.       Index           =   2
  126.       Left            =   0
  127.       TabIndex        =   4
  128.       Top             =   525
  129.       Width           =   615
  130.    End
  131.    Begin VB.Label Label1 
  132.       Caption         =   "dt"
  133.       Height          =   255
  134.       Index           =   1
  135.       Left            =   0
  136.       TabIndex        =   1
  137.       Top             =   60
  138.       Width           =   255
  139.    End
  140.    Begin VB.Menu mnuFile 
  141.       Caption         =   "&File"
  142.       Begin VB.Menu mnuFileExit 
  143.          Caption         =   "E&xit"
  144.       End
  145.    End
  146. End
  147. Attribute VB_Name = "BDitch2Form"
  148. Attribute VB_Creatable = False
  149. Attribute VB_Exposed = False
  150. Option Explicit
  151.  
  152. Const PI = 3.14159
  153. Const TWO_PI = 2 * PI
  154.  
  155. Dim P As Integer
  156. Dim Q As Integer
  157.  
  158. ' ************************************************
  159. ' Draw the curve on the indicated picture box.
  160. ' ************************************************
  161. Sub DrawCurve(pic As PictureBox, start_t As Single, stop_t As Single, Dt As Single, xscale As Single, yscale As Single, theta As Single)
  162. Dim x1 As Single
  163. Dim y1 As Single
  164. Dim x2 As Single
  165. Dim y2 As Single
  166. Dim ctheta As Single
  167. Dim stheta As Single
  168. Dim t As Single
  169.  
  170.     ' Save these values because we use them a lot.
  171.     stheta = Sin(theta)
  172.     ctheta = Cos(theta)
  173.     
  174.     x1 = xscale * X(start_t)
  175.     y1 = yscale * Y(start_t)
  176.     x2 = x1 * ctheta - y1 * stheta
  177.     y2 = x1 * stheta + y1 * ctheta
  178.     pic.Cls
  179.     pic.CurrentX = x2
  180.     pic.CurrentY = y2
  181.     
  182.     t = start_t + Dt
  183.     Do While t < stop_t
  184.         x1 = xscale * X(t)
  185.         y1 = yscale * Y(t)
  186.         x2 = x1 * ctheta - y1 * stheta
  187.         y2 = x1 * stheta + y1 * ctheta
  188.         pic.Line -(x2, y2)
  189.         t = t + Dt
  190.     Loop
  191.     
  192.     x1 = xscale * X(stop_t)
  193.     y1 = yscale * Y(stop_t)
  194.     x2 = x1 * ctheta - y1 * stheta
  195.     y2 = x1 * stheta + y1 * ctheta
  196.     pic.Line -(x2, y2)
  197. End Sub
  198.  
  199. ' ************************************************
  200. ' Non-recursively compute the greatest common
  201. ' divisor of to integers.
  202. ' ************************************************
  203. Private Function GCD(ByVal a As Integer, ByVal b As Integer) As Integer
  204. Dim B_Mod_A As Integer
  205.  
  206.     B_Mod_A = b Mod a
  207.     Do While B_Mod_A <> 0
  208.         ' Prepare the arguments for the "recursion."
  209.         b = a
  210.         a = B_Mod_A
  211.         B_Mod_A = b Mod a
  212.     Loop
  213.  
  214.     GCD = a
  215. End Function
  216.  
  217.  
  218. ' ************************************************
  219. ' Find the least common multiple of two integers.
  220. ' ************************************************
  221. Function LCM(a As Integer, b As Integer) As Integer
  222.     LCM = a * b / GCD(a, b)
  223. End Function
  224.  
  225.  
  226. ' ************************************************
  227. ' Calculate the values t must cross to draw a
  228. ' Bowditch Curve.
  229. ' ************************************************
  230. Sub SetTBounds(tmin As Single, tmax As Single)
  231.     tmin = 0
  232.     tmax = LCM(P, Q) / P / Q * TWO_PI
  233.     If P Mod 2 = 1 And Q Mod 2 = 1 Then
  234.         tmin = -tmax / 4
  235.         tmax = tmax / 4
  236.     End If
  237. End Sub
  238.  
  239.  
  240.  
  241. ' ************************************************
  242. ' The parametric function X(t).
  243. ' ************************************************
  244. Function X(t As Single) As Single
  245.     X = Sin(P * t)
  246. End Function
  247. ' ************************************************
  248. ' The parametric function Y(t).
  249. ' ************************************************
  250. Function Y(t As Single) As Single
  251.     Y = Sin(Q * t)
  252. End Function
  253.  
  254. Private Sub CmdGo_Click()
  255. Dim tmin As Single
  256. Dim tmax As Single
  257. Dim Dt As Single
  258. Dim xscale As Single
  259. Dim yscale As Single
  260. Dim theta As Single
  261.  
  262.     P = CInt(PText.Text)
  263.     Q = CInt(QText.Text)
  264.     
  265.     SetTBounds tmin, tmax
  266.     
  267.     Dt = CSng(DtText.Text)
  268.     xscale = CSng(XscaleText.Text)
  269.     yscale = CSng(YscaleText.Text)
  270.     theta = CSng(ThetaText.Text) / 180 * PI
  271.     
  272.     DrawCurve Canvas, tmin, tmax, Dt, xscale, yscale, theta
  273. End Sub
  274.  
  275.  
  276. Private Sub mnuFileExit_Click()
  277.     Unload Me
  278. End Sub
  279.  
  280.  
  281.